package us.mmapp;


import java.util.HashMap;

import us.mmapp.entities.FriendEntity;
import us.mmapp.entities.ObjectEntity;
import us.mmapp.util.ApplicationData;
import us.mmapp.util.DBOpenHelper;
import us.mmapp.util.FileNameUtil;
import us.mmapp.util.ThumbnailUtil;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.CursorIndexOutOfBoundsException;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class CommentActivity extends Activity implements OnClickListener
{
	int SID;
	TextView textView;
	EditText editText;
	StringBuilder sb;
	boolean first;
	String mediaSource;
	int type;
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.comment);
		Bundle bundle=this.getIntent().getExtras();
		SID=bundle.getInt("keyword");
		ImageView iv=(ImageView)findViewById(R.id.image);
		ThumbnailUtil.setImageView(this, SID, iv, true);
		textView=(TextView)findViewById(R.id.info);
		editText=(EditText)findViewById(R.id.new_comment_edit);
		editText.setHint("Enter a new Comment Here!");
		
		//hide keyboard if first time load
		getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
		
		Button b=(Button)findViewById(R.id.done_button);
		iv.setOnClickListener(this);
		b.setOnClickListener(this);
		b=(Button)findViewById(R.id.add_comment_button);
		b.setOnClickListener(this);
		sb=new StringBuilder();
		DBOpenHelper data=new DBOpenHelper(this, "mmAppDB", DBOpenHelper.VERSION);
		SQLiteDatabase db=data.getReadableDatabase();
		Cursor c=db.rawQuery("Select * from objects where "+DBOpenHelper.SID+"="+SID, null);
		if(c.moveToNext())
		{
			int uid=c.getInt(DBOpenHelper.uid);
			HashMap<Long, FriendEntity> friends=ApplicationData.getFriends();
			String userName="";
			if(friends!=null)
			{
				FriendEntity f=friends.get(new Long(uid));
				if(f!=null)
					userName=f.getUsername();
			}
			String fileName=c.getString(0);
			if(!userName.equals(""))
				sb.append("Owner:\n"+userName+"\n\n");
			else
				sb.append("Owner:\n"+ApplicationData.getUsername()+"\n\n");
			if(c.getString(3) == null || c.getString(3).equals(""))
				mediaSource=c.getString(12);
			else
				mediaSource=c.getString(3);
			type=c.getInt(4);
			sb.append("Title:\n"+c.getString(5)+"\n\n");
			sb.append("Description:\n"+c.getString(6)+"\n\n");
			sb.append("Latitude:\n"+c.getDouble(10)+"\n\n");
			sb.append("Longitude:\n"+c.getDouble(11)+"\n\n");
			c.close();
			c=db.rawQuery("Select * from tags where ID='"+fileName+"'", null);
			try{
				sb.append("Tags:"+c.getString(1)+"\n");
			}
			catch(CursorIndexOutOfBoundsException e)
			{
				Log.i("Comment Activity", "No tags");
			}
			c.close();
			c=db.rawQuery("Select * from extra_fields where oid="+SID+" order by order_id", null);
			while(c.moveToNext())
			{
				sb.append(c.getString(0)+":\n");
				sb.append(c.getString(1)+"\n\n");
			}
			c.close();
			c=db.rawQuery("Select * from comments where oid="+SID+" order by time", null);
			first=true;
			while(c.moveToNext())
			{
				if(first)
				{
					sb.append("Comments:\n");
					first=false;
				}
				sb.append("\u2022"+c.getString(3)+"\n");
			}
			c.close();
			textView.setText(sb);
		}
		db.close();

	}
	
	@Override
	public void onClick(View v)
	{
		if(v.getId()==R.id.image)
		{
			Toast.makeText(this, type+"", Toast.LENGTH_LONG);
			if(type==FileNameUtil.VIDEO || type==FileNameUtil.AUDIO)
			{
				Intent tostart = new Intent(Intent.ACTION_VIEW);
				tostart.setDataAndType(Uri.parse(mediaSource), "video/*");
				startActivity(tostart);
			}
			else if(type==FileNameUtil.PICTURE)
			{
				
				Intent intent=new Intent();
				intent.setClass(this, ViewImageActivity.class);
				Bundle bundle=new Bundle();
				bundle.putInt("keyword", SID);
				intent.putExtras(bundle);
				startActivity(intent);
			}

		}
		else if(v.getId()==R.id.add_comment_button)
		{
			String newComment=editText.getText().toString();
			if(newComment.equals(""))
			{
				InfoActivity.alertPopup("Comment Error", "Can not add an empty comment.", this);
			}
			else if(!InfoActivity.validInput(newComment))
			{
				InfoActivity.alertPopup("Comment Error", "Only use letters, numbers, spaces, and @.-_+!", this);
			}
			else
			{
				SharedPreferences settings = getSharedPreferences("LOGIN", 0);
				DBOpenHelper data = new DBOpenHelper(this, "mmAppDB", DBOpenHelper.VERSION);
				SQLiteDatabase db=data.getWritableDatabase();
				ContentValues cv=new ContentValues();
				cv.put("OID", SID);
				cv.put("UID", settings.getLong("uid", 0));
				cv.put("TIME", System.currentTimeMillis());
				cv.put("COMMENT", newComment);
				db.insertWithOnConflict("Comments", null, cv, SQLiteDatabase.CONFLICT_REPLACE);
				editText.setText("");
				if(first)
				{
					sb.append("Comments:\n");
					first=false;
				}
					
				sb.append("\u2022"+newComment+"\n");
				textView.setText(sb);
				db.close();
				db=data.getReadableDatabase();
				Cursor c=db.rawQuery("Select ID from objects where ID="+SID, null);
				c.moveToLast();
				ObjectEntity o=new ObjectEntity(this, c.getLong(c.getColumnIndex(DBOpenHelper.ID)));
				o.setSynced(false);
				o.insertDB(this);
			}
		}
		else if(v.getId()==R.id.done_button)
		{
			finish();
		}
	}
}
